<?php
preg_match("/.+\/(.*)/", $_SERVER['SCRIPT_NAME'], $self_name);

include_once '../info_struct.php';
include_once '../LDB_API/input.php';
include_once '../LDB_API/output.php';

function get_id() {
    $data = trim($_GET['id']);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysql_real_escape_string($data);
    return $data;
}

function POST($str) {

    return htmlspecialchars($_POST[$str]);

}

$info_struct = new infos();

if (isset($_POST['id']))
    $info_struct->id = POST('id');

if (isset($_POST['title']))
    $info_struct->title = POST('title');
if (isset($_POST['year']))
    $info_struct->year = POST('year');
if (isset($_POST['author']))
    $info_struct->author = POST('author');

if (isset($_POST['note']))
    $info_struct->note = POST('note');

if (isset($_POST['doi']))
    $info_struct->doi = POST('doi');
if (isset($_POST['zmath']))
    $info_struct->zmath = POST('zmath');
if (isset($_POST['arxiv']))
    $info_struct->arxiv = POST('arxiv');
if (isset($_POST['number']))
    $info_struct->number = POST('number');
if (isset($_POST['volume']))
    $info_struct->volume = POST('volume');
if (isset($_POST['journal']))
    $info_struct->journal = POST('journal');
if (isset($_POST['pages']))
    $info_struct->pages = POST('pages');
if (isset($_POST['month']))
    $info_struct->month = POST('month');
if (isset($_POST['other']))
    $info_struct->other = POST('other');
if (isset($_POST['bc']) && POST('bc') === "yes")
    $info_struct->bc = true;

if (isset($info_struct->title) && isset($info_struct->author) && isset($info_struct->year)) {
    $reference_id = add_ref($info_struct);
    if ($reference_id)
        die("Success");
    else
        die("Failure");
}

if (isset($_GET['id']))
    $ref = ldb_get_by_id(get_id());
if ($ref) {
    ?>
<!DOCTYPE html>
<html class="no-js fuelux">
<head>

<title>References Wizard</title>

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

<link href="http://www.fuelcdn.com/fuelux/2.6.0/css/fuelux.min.css"
	rel="stylesheet" type="text/css">

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://www.fuelcdn.com/fuelux/2.6.1/loader.min.js"
	type="text/javascript"></script>
</head>

<body>
	<form id="form_fix"
		action="<?php echo (isset($self_name[1]) ? $self_name[1] : "update_db_e.php" )?>"
		method="post">
		<table>
			<tr>
				<td>Title:</td>
				<td class="man_input"><input type="text" name="title"
					style="margin: 0px;"
					value="<?php echo isset($ref->title) ? $ref->title : '' ?>"></td>
			</tr>
			<tr>
				<td>Year:</td>
				<td class="man_input"><input type="text" name="year"
					style="margin: 0px;"
					value="<?php echo isset($ref->year) ? $ref->year : '' ?>"> <label
					class="checkbox checkbox-custom"><input type="checkbox" name="bc"
						value="yes"><i class="checkbox"></i>before Christ</label></td>

			</tr>
			<tr>
				<td>Author:</td>
				<td class="man_input"><input type="text" name="author"
					style="margin: 0px;"
					value="<?php echo isset($ref->author) ? $ref->author : '' ?>"></td>
			</tr>
			<tr>
				<td>DOI:</td>
				<td class="man_input"><input type="text" name="doi"
					style="margin: 0px;"
					value="<?php echo isset($ref->doi) ? $ref->doi : '' ?>"></td>

			</tr>
			<tr>
				<td>ZMath:</td>
				<td class="man_input"><input type="text" name="zmath"
					style="margin: 0px;"
					value="<?php echo isset($ref->zmath) ? $ref->zmath : '' ?>"></td>

			</tr>
			<tr>
				<td>Arxiv:</td>
				<td class="man_input"><input type="text" name="arxiv"
					style="margin: 0px;"
					value="<?php echo isset($ref->arxiv) ? $ref->arxiv : '' ?>"></td>

			</tr>
			<tr>
				<td>Journal:</td>
				<td class="man_input"><input type="text" name="journal"
					style="margin: 0px;"
					value="<?php echo isset($ref->journal) ? $ref->journal : '' ?>"></td>

			</tr>
			<tr>
				<td>Volume:</td>
				<td class="man_input"><input type="text" name="volume"
					style="margin: 0px;"
					value="<?php echo isset($ref->volume) ? $ref->volume : '' ?>"></td>

			</tr>
			<tr>
				<td>Number:</td>
				<td class="man_input"><input type="text" name="number"
					style="margin: 0px;"
					value="<?php echo isset($ref->number) ? $ref->number : '' ?>"></td>

			</tr>
			<tr>
				<td>Pages:</td>
				<td class="man_input"><input type="text" name="pages"
					style="margin: 0px;"
					value="<?php echo isset($ref->pages) ? $ref->pages : '' ?>"></td>

			</tr>
			<tr>
				<td>Month:</td>
				<td class="man_input"><input type="text" name="month"
					style="margin: 0px;"
					value="<?php echo isset($ref->month) ? $ref->month : '' ?>"></td>

			</tr>
			<tr>
				<td>Other:</td>
				<td class="man_input"><input type="text" name="other"
					style="margin: 0px;"
					value="<?php echo isset($ref->other) ? $ref->other : '' ?>"></td>

			</tr>
		</table>
		<input type="hidden" name="id" value="<?=$ref->id ?>">
		<textarea id="note" name="note" cols="50" rows="10">
			<?php echo isset($ref->note) ? $ref->note : '' ?>
		</textarea>
		<input class="btn" type="submit" value="Submit" />
	</form>
</body>
</html>
<?php 
} else {

	echo "Reference ID not found.";

}
?>